﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TelerikStore
{
    /// <summary>
    /// Summary description for ShoppingCart
    /// </summary>
    public class ShoppingCart
    {
        #region Properties
        private List<ProductItem> _items;
        public List<ProductItem> Items
        {
            get { return this._items; }
        }

        public int TotalProductsCount
        {
            get
            {
                int result = 0;
                foreach (ProductItem item in this._items)
                {
                    result += item.Quantity;
                }

                return result;
            }
        }

        public double TotalProductsPrice
        {
            get
            {
                double result = 0;
                foreach (ProductItem item in this._items)
                {
                    result += ((double)item.Price * (double)item.Quantity);
                }

                return result;
            }
        }
        #endregion

        #region Methods
        public void Add(ProductItem item)
        {
            this.AddWithQuantity(item, 1);
        }

        public void AddWithQuantity(ProductItem item, int quantity)
        {
            ProductItem cItem = this._GetExistingItem(item);
            if (cItem != null)
                cItem.Quantity += quantity;
            else
            {
                item.Quantity = quantity;
                this._items.Add(item);
            }
        }

        public void AddAll(List<ProductItem> itemsList)
        {
            foreach (ProductItem item in itemsList)
            {
                if (item.Quantity > 0)
                    this.AddWithQuantity(item, item.Quantity);
                else
                    this.Add(item);
            }
        }

        public void Remove(ProductItem item)
        {
            this.RemoveWithQuantity(item, 1);
        }

        public void RemoveWithQuantity(ProductItem item, int quantity)
        {
            ProductItem cItem = this._GetExistingItem(item);

            if (cItem != null && cItem.Quantity > 1 && cItem.Quantity != quantity)
                cItem.Quantity -= quantity;
            else
            {
                this._items.Remove(item);
            }
        }

        public void RemoveAll(List<ProductItem> itemsList)
        {
            foreach (ProductItem item in itemsList)
            {
                if (item.Quantity > 0)
                    this.RemoveWithQuantity(item, item.Quantity);
                else
                    this.Remove(item);
            }
        }

        public void Empty()
        {
            this._items.Clear();
        }

        public void SubmitOrder()
        { 
            //ToDo

            this.Empty();
        }

        #region Private Methods
        private ProductItem _GetExistingItem(ProductItem item)
        {
            return this._items.Find(
                delegate(ProductItem cItem)
                {
                    return cItem.Id == item.Id;
                });
        }
        #endregion
        #endregion

        #region Constructors
        public ShoppingCart()
	    {
            this._items = new List<ProductItem>();
	    }

        public ShoppingCart(List<ProductItem> Items)
        {
            this._items = new List<ProductItem>(Items);
        }
        #endregion

    }
}